<template>
  <div class="login-wrap">
    <div class="rl-modal">
      <div class="js-forgot-result forgot-send-result forgot-modal"  >
        <span class="icon-send-success">
          <i class="imv2-check"></i>
        </span>
        <p>恭喜您支付成功</p>
<!--        <div class="get-info">已发送至-->
<!--          <span class="xa-email">{{form.mobile}}</span>-->
<!--        </div>-->
        <router-link :to="{path:'/uc/myCourse',query:{courseType:''}}" class="moco-btn moco-btn-red moco-btn-lg js-email-add">前往个人中心</router-link>
<!--        <div class="get-info">收不到邮件？查看-->
<!--          <a href="javascript:void(0)" target="_blank">常见问题</a>-->
<!--        </div>-->
      </div>

    </div>
  </div>
</template>

<script>
  import {captcha,sendEmailResetPwd,sendSMSByMobile,checkCaptcha,updatePwdByMobile} from "@/api";
  import { mapGetters } from "vuex";
  import { isEmail,isPassword,validatenull,isMobile} from '@/util/validate';

  export default {
    name: 'Login',
    data() {
      return {
        step:1,
        reSendTime:0,
        showPassword:false,
        reSendMsg:"重新发送",
        captchaImg:"",
        captchaKey:"",
        code:"",
        form:{
          mobile:"",
          code:"",
          key:"",
          newPwd:""
        },
        formMsg:{
          mobileMsg:"",
          codeMsg:"",
          error:"",
          smsMsg:"",
          newPwdMsg:"",
          error1:""
        }
      }
    },
    methods: {
      updatePwd(){
        //判断验证码是否为空
        if(validatenull(this.code)) {
          this.formMsg.smsMsg = "请输入短信验证码";
          return;
        }
        //判断密码是否符合规则
        if(isPassword(this.form.newPwd)) {
          this.formMsg.newPwdMsg = "请输入8-20位字母、数字或字符，至少包含两种！";
          return;
        }
        this.form.code = this.code
        updatePwdByMobile(this.form).then(res=>{
          this.step = 4
        },error=>{
          this.formMsg.error1 = error.message
        })
      },
      reSendSMS(){
        if(this.reSendTime == 0){
          //发送手机验证码
          sendSMSByMobile(this.form.mobile,"common_code").then(res =>{
            this.reSendTime = 60
            let that = this
            let timer = setInterval(function () {
              that.reSendTime = that.reSendTime-1
              that.reSendMsg = "重新发送  "+ that.reSendTime;
              if(that.reSendTime == 0){
                clearInterval(timer);
                that.reSendMsg = "重新发送";
              }
            },1000)
          }, error => {
            this.errCodeMsg = error.message;
          });
        }
      },
      switchPassword(){
        this.showPassword = !this.showPassword
      },
      init(){
        this.formMsg.mobileMsg = ""
        this.formMsg.codeMsg = ""
        this.formMsg.error = ""

        this.clickCaptcha()
      },
      clickCaptcha(){
        captcha().then(res =>{
          this.captchaImg = res.data.data.image
          this.captchaKey = res.data.data.key
        })
      },
      goStep1(){
        this.formMsg.mobileMsg = ""
        this.formMsg.codeMsg = ""
        this.formMsg.error = ""
        this.step = 1;
      },
      next(){

        this.formMsg.mobileMsg = ""
        this.formMsg.codeMsg = ""
        if(validatenull(this.form.mobile)) {
          this.formMsg.mobileMsg = "手机号/邮箱不能为空！";
          return;
        }
        if(validatenull(this.form.code)) {
          this.formMsg.codeMsg = "请输入正确的验证码！";
          return;
        }
        //是手机
        if(isMobile(this.form.mobile)){

          //验证验证码
          let param = {
            "key":this.captchaKey,
            "code":this.form.code
          }
          checkCaptcha(param).then(res =>{
            //发送手机验证码
            sendSMSByMobile(this.form.mobile,"common_code").then(res=>{
              this.step = 3;
            })
          }, error => {
            this.formMsg.codeMsg = error.message;
          });
          return;
        }
        //是邮箱
        if(isEmail(this.form.mobile)){
          this.form.email = this.form.mobile
          this.form.key = this.captchaKey
          sendEmailResetPwd(this.form).then(res =>{
            this.step = 2
            this.$store.dispatch("LogOut")
          },error =>{
            this.formMsg.error = error.message
          })
          return;
        }
        this.formMsg.mobileMsg = "请输入正确的手机号/邮箱！";
        return;
      }
    },
    mounted:function() {
      this.init()
    },

  }
</script>

<style scoped>

</style>
